package com.cango.qutz.web.auth.org.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cango.qutz.web.auth.org.service.IOrgService;
import com.cango.qutz.web.entity.OrgEntity;
import com.cango.framework.base.AjaxReturnVO;
import com.cango.framework.base.controller.BaseController;
import com.cango.framework.base.pagination.DataTableRequest;
import com.cango.framework.base.pagination.DataTableResponse;
import com.cango.framework.base.pagination.PageInfo;
import com.cango.framework.base.pagination.PaginationResult;
import com.cango.qutz.web.auth.org.service.IOrgService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;

/**
 * <p>
 * ClassName: OrgController
 * </p>
 * <p>
 * Description: 机构Controller
 * </p>
 * <p>
 * Author: weih
 * </p>
 * <p>
 * Date: 2015-04-01
 * </p>
 */
@Controller
@RequestMapping("/auth/org")
public class OrgController extends BaseController {

    /**
     * log
     */
    private final Log log = LogFactory.getLog(OrgController.class);

    /**
     * <p>
     * Field orgService: 机构Service注入
     * </p>
     */
    @Autowired
    private IOrgService orgService;

    /**
     * <p>
     * Description: 到机构代码树的页面
     * </p>
     * 
     * @return 页面
     */
    @RequestMapping("/toOrgIndex")
    public String toOrgIndex() {
        System.out.println();
        return "/organization/organ_zTree";
    }

    /**
     * <p>
     * Description: 到机构代码列表页面
     * </p>
     * 
     * @return 页面
     */
    @RequestMapping("/toOrgList")
    public String toOrgList(String parentCode, Model model) {
        // 设置父节点代码
        model.addAttribute("parentCode", parentCode);
        return "/organization/organ_list";
    }

    /**
     * <p>
     * Description: 得到机构树的数据
     * </p>
     * 
     * @return 页面
     */
    @RequestMapping("/getOrganTreeData")
    public void getOrganTreeData(HttpServletRequest request, HttpServletResponse response) {
        List<OrgEntity> organList = this.orgService.queryList(new Object());
        this.ajaxJson(response, organList);
    }

    /**
     * <p>
     * Description: 得到机构列表的数据
     * </p>
     * 
     * @return 页面
     */
    @RequestMapping("/getOrgListByParent")
    @ResponseBody
    public DataTableResponse<OrgEntity> getOrgListByParent(OrgEntity organ, DataTableRequest dataTable) {
        PageInfo pageInfo = new PageInfo(dataTable.getCurrentPage(), dataTable.getiDisplayLength());
        PaginationResult<OrgEntity> result = this.orgService.queryListByPage(organ, pageInfo);
        return new DataTableResponse(dataTable.getsEcho(), result);
    }

    /**
     * <p>
     * Description: 跳转机构新增页面
     * </p>
     * 
     * @return
     */
    @RequestMapping("/toAddOrg")
    public String toAddOrg(String parentCode, Model model) {
        //查询上级机构信息
        OrgEntity orgInfo = this.orgService.queryOrgByCode(parentCode);
        model.addAttribute("orgInfo", orgInfo);
        return "/organization/organ_add";
    }

    /**
     * 功能描述: <br>
     * 验证机构代码唯一
     *
     * @param orgCode 机构代码
     * @return 是否唯一
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/validOrgCode")
    @ResponseBody
    public boolean validOrgCode(String orgCode) {
        return this.orgService.validOrgCode(orgCode);
    }

    /**
     * <p>
     * Description: 跳转机构列表页面
     * </p>
     * 
     * @param org 机构对象
     * @return
     */
    @RequestMapping("/list")
    public String list(OrgEntity org) {
        return "org/orgList";
    }

    /**
     * <p>
     * Description: 分页查询机构记录
     * </p>
     * 
     * @param org 机构对象
     * @param response response
     * @param pageInfo 分页对象
     */
    @RequestMapping("/getListData")
    public void getListData(OrgEntity org, HttpServletResponse response, PageInfo pageInfo) {
        PaginationResult<OrgEntity> result = orgService.queryListByPage(org, pageInfo);
        super.ajaxJson(response, result);
    }

    /**
     * <p>
     * Description: 保存机构记录
     * </p>
     * 
     * @param org 机构对象
     * @param response
     */
    @RequestMapping("/saveOrg")
    public void saveOrg(OrgEntity org, HttpServletResponse response) {
        org.setCreateUser(getSessionUser().getUserId());
        org.setCreateTime(new Date());
        orgService.insert(org);
        log.info("用户" + getSessionUser().getUserName() + "新增了机构:" + (new Gson()).toJson(org));
        super.ajaxJson(response, AjaxReturnVO.success());
    }

    /**
     * <p>
     * Description: 跳转机构编辑页面
     * </p>
     * 
     * @param org 机构对象
     * @param model
     * @return
     */
    @RequestMapping("toEditOrg")
    public String toEditOrg(Integer orgId, Model model) {
        // 根据ID查询机构信息
        OrgEntity organ = this.orgService.queryObjectById(orgId);
        // 查询上级机构的信息
        String parentName = null;
        if (null != organ.getParentCode() && !"".equals(organ.getParentCode())) {
            OrgEntity organParent = this.orgService.queryOrgByCode(organ.getParentCode());
            parentName = organParent.getOrgName();
        }
        model.addAttribute("parentName", parentName);
        model.addAttribute("organ", organ);
        return "organization/organ_edit";
    }

    /**
     * <p>
     * Description: 保存机构修改记录
     * </p>
     * 
     * @param org 机构对象
     * @param response
     */
    @RequestMapping("/editOrg")
    public void editOrg(OrgEntity org, HttpServletResponse response) {
        org.setUpdateUser(getSessionUser().getUserId());
        orgService.update(org);
        log.info("用户" + getSessionUser().getUserName() + "修改了机构:" + (new Gson()).toJson(org));
        super.ajaxJson(response, AjaxReturnVO.success());
    }

    /**
     * <p>
     * Description: 删除机构记录
     * </p>
     * 
     * @param org 机构对象
     * @param response
     */
    @RequestMapping("deleteOrg")
    public void deleteOrg(Integer orgId, HttpServletResponse response) {
        OrgEntity org = new OrgEntity();
        org.setOrgId(orgId);
        org.setUpdateUser(getSessionUser().getUserId());
        org.setUpdateTime(new Date());
        orgService.delete(org);
        log.info("用户" + getSessionUser().getUserName() + "删除了机构:" + (new Gson()).toJson(org));
        super.ajaxJson(response, AjaxReturnVO.success());
    }
    
    /**
     * <p>
     * Description: 得到所有分区包括总部
     * 用于合同归属select
     * </p>   
     * 
     * @return 页面
     */
    @RequestMapping("/getOrganRegion")
    public void getOrganRegion(HttpServletRequest request, HttpServletResponse response) {
    	OrgEntity org=new OrgEntity();
    	org.setParentCode("anji_10");//总部
    	org.setOrgCode("anji_0");//分区
        List<OrgEntity> organList = this.orgService.queryOrgRegionList(org);
        this.ajaxJson(response, organList);
    }
}
